package com.example.yrdev.ordering_system.Main_Menu;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.homeOptions.MainActivity;
import com.example.yrdev.ordering_system.homeOptions.MenuActivity;

//import org.androidannotations.annotations.ViewById;


import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by yrdev on 9/1/16.
 */

public class MenuRechargeDialog extends Dialog implements View.OnClickListener{


@InjectView(R.id.recharge_status)
LinearLayout recharge_status;
@InjectView(R.id.recharge_val) EditText recharge_val;

    @InjectView(R.id.recharge_but_layout)LinearLayout recharge_but_layout;
    @InjectView(R.id.otherInfo)LinearLayout other_info;
    @InjectView(R.id.otherInfo1)LinearLayout other_info1;
    @InjectView(R.id.prevAmount)TextView prevAmount_text;
    @InjectView(R.id.curAmount)TextView curAmount_text;
    @InjectView(R.id.chargeBalance)TextView chargeBalance;

Context context;
private Callback callback;

    public interface Callback{
        void getAmount(Integer amount);

    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public MenuRechargeDialog(Context context){
        super(context);
    this.context=context;

        requestWindowFeature(Window.FEATURE_NO_TITLE);

   }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.menu_recharge_layout);
        ButterKnife.inject(this);

        recharge_status.setVisibility(View.GONE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);


        recharge_val.setOnEditorActionListener(keyboardEditor());
        other_info.setVisibility(View.GONE);
    }


    @OnClick(R.id.recharge)
    public void recharge(){

        try {

            int value=GlobalFunction.convertToInt(recharge_val.getText().toString());

            if (value>0){

                editMode(false);

            }else {

                GlobalFunction.showKeyboardForEditText(context,recharge_val);
                GlobalFunction.ToastAlert(getContext(),"请输入充值余额");
            }

        }catch (Exception e){
            GlobalFunction.ToastAlert(getContext(),"请输入充值余额");
        }



    }

    public void showOtherInfo(Boolean stat,Double prevAmount,Double curAmount){
        if (stat){

            other_info.setVisibility(View.VISIBLE);
            other_info1.setVisibility(View.GONE);
            recharge_val.setVisibility(View.GONE);


            String balance=String.valueOf(recharge_val.getText().toString())+" 元";
            chargeBalance.setText(balance);

            String str=String.valueOf(prevAmount)+" 元";
            prevAmount_text.setText(str);

            String str2=String.valueOf(curAmount)+" 元";
            curAmount_text.setText(str2);



            new java.util.Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    MenuActivity activity = (MenuActivity) context;
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showOtherInfo(false, 0., 0.);
                        }
                    });
                    // GlobalFunction.ToastAlert(context,"ccasd");

                }

            }, 2000);

        }

        else {

            recharge_val.setVisibility(View.VISIBLE);
            other_info1.setVisibility(View.VISIBLE);
            other_info.setVisibility(View.GONE);

        }
    }


    public void showRechargeButton(boolean stat){
        if (stat){
           recharge_but_layout.setVisibility(View.VISIBLE);
            recharge_status.setVisibility(View.GONE);
        }else {
            recharge_status.setVisibility(View.VISIBLE);
            recharge_but_layout.setVisibility(View.GONE);

        }
    }
    public void raiseCallback(Double amount){
        if (amount==0)
        {
            recharge_val.setText("");
            recharge_val.setEnabled(true);
            GlobalFunction.showKeyboardForEditText(context,recharge_val);
        }


        if (callback!=null){
            amount=amount*100;
            callback.getAmount(amount.intValue());
        }

    }
    public void editMode(Boolean stat){

        if (stat){
            recharge_val.setEnabled(true);
            recharge_val.requestFocus();
            raiseCallback(0.);
//            GlobalFunction.ToastAlert(context, "请确认此消费余额");
//            GlobalFunction.showKeyboardForEditText(context, price);
//            swipe_stat.setText("请输入消费余额");
        }else {
            recharge_val.setEnabled(false);
            raiseCallback(Double.valueOf(recharge_val.getText().toString()));

//            if (callBack!=null)
//                callBack.getCurAmount(Integer.valueOf(price.getText().toString()));
          showRechargeButton(false);

            new java.util.Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    MenuActivity activity = (MenuActivity) context;
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showRechargeButton(true);

                            raiseCallback(0.);

                        }
                    });
                    // GlobalFunction.ToastAlert(context,"ccasd");

                }

            }, 3000);
        }

    }


    private TextView.OnEditorActionListener keyboardEditor(){
        TextView.OnEditorActionListener editorActionListener=new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_DONE)
                {

                    try {
                        Double price_val=Double.valueOf(recharge_val.getText().toString());

                        if (price_val<=0.){
                            GlobalFunction.printTextByTag(price_val+"");
                            editMode(true);
                            GlobalFunction.ToastAlert(context, "请确认此充值余额");

                        }else {
                           // savePurchasePrice(price_val);
                            editMode(false);
                        }

                    }catch (Exception e){

                        GlobalFunction.printTextByTag(e.getMessage());
                        GlobalFunction.ToastAlert(context,"请确认此充值余额");
                        GlobalFunction.showKeyboardForEditText(context,recharge_val);
                    }

                }

                return true;
            }
        };
        return editorActionListener;
    }
    @OnClick(R.id.close)
    public void closeDialog(){
        this.dismiss();

    }

    @Override
    public void onClick(View view) {

    }
}
